<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/page/common/commonConfig.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>
    <base href="<%=basePath%>">
    <title>业务角色查询</title>
    <script src="<%=basePath%>component/layout.js"></script>
    <script src="<%=basePath%>component/toolbar.js"></script>
    <script src="<%=basePath%>component/grid.js"></script>
    <script src="<%=basePath%>dwrsystem/engine.js"></script>
	<script src="<%=basePath%>dwrsystem/interface/roleBusinessService.js"></script>
	<script type="text/javascript" src="<%=basePath%>dwrsystem/engine.js"></script>
	<script type="text/javascript" src="<%=basePath%>dwrsystem/interface/UserRolebusinessAuthorize.js"></script>
    <script type="text/javascript">
	var mygrid;
	var addUserWindow;
	var dhxLayout = createLayoutNormal('1C');
	
	var dhxGrid = {
		initDataTable : function () {
		    mygrid.setHeader("序号,业务角色名称,备注");
		    mygrid.setInitWidths("50,200,*");
		    mygrid.setColAlign("right,left,left");
		    mygrid.setColSorting("int,str,str");
		    mygrid.setColTypes(power.getPowerUpdate()?"ro,ed,ed":"ro,ro,ro");
		    mygrid.attachEvent("onEditCell", this.onEditCell);
		    mygrid.init();
		    setSkin(mygrid);
		    mygrid.loadXML('<%=basePath%>system-roleBusiness/RoleBusinessAction!roleBusinessGrid.action');
		},
		onEditCell : function (stage,rId,cInd,nValue,oValue){
			if(stage == 2 && nValue != oValue){
				if(cInd == '1'){
					layoutProgressOn(dhxLayout);
					roleBusinessService.updateTRoleBusinessByPrimaryKey({roleBusinessId:rId,roleBusinessName:nValue},function(data){
						parent.notifyCreater('业务角色名称修改成功');
						layoutProgressOff(dhxLayout);
					});
				}
				if(cInd == '2'){
					layoutProgressOn(dhxLayout);
					roleBusinessService.updateTRoleBusinessByPrimaryKey({roleBusinessId:rId,note:nValue},function(data){
						parent.notifyCreater('备注修改成功');
						layoutProgressOff(dhxLayout);
					});
				}
		   	}
		    return true;
		},
		createInfo : function (){
			layoutProgressOn(dhxLayout);
			roleBusinessService.insertByTRoleBusiness({roleBusinessId:''},function(data){
				var sortnumber = mygrid.getRowsNum()+1;
				mygrid.addRow(data, ''+(mygrid.getRowsNum()+1), ''+mygrid.getRowsNum());
				mygrid.selectRowById(data);
				parent.notifyCreater('添加成功');
				layoutProgressOff(dhxLayout);
			});
		},
		deleteInfo : function (){
			if(mygrid.getSelectedId()){
				confirmInfo('确认删除？',
					function(choose){
						if('ok' == choose){
							layoutProgressOn(dhxLayout);
							roleBusinessService.deleteTRoleBusinessByPrimaryKey({roleBusinessId:mygrid.getSelectedId()},function(data){
								var lastrowIndex = mygrid.getRowIndex(mygrid.getSelectedId())-1;
								mygrid.deleteSelectedRows();
								mygrid.selectRow(lastrowIndex);
								
								//修改前面的序号
								for(var i=(lastrowIndex+1);i<(mygrid.getRowsNum());i++){
									mygrid.cells(mygrid.getRowId(''+i),0).setValue(i+1);
								}
								parent.notifyCreater('删除成功');
								layoutProgressOff(dhxLayout);
							});
						}
					}
				);
			} else parent.notifyWarn('请选择删除项');
		},
		rowUp : function(){
			var rowId = mygrid.getSelectedId();
			if(rowId) {
				this.sort('up',rowId);
			}else parent.notifyWarn('请选择移动项');
		},
		rowDown : function(){
			var rowId = mygrid.getSelectedId();
			if(rowId){
				this.sort('down',rowId);
			}else parent.notifyWarn('请选择移动项');
		},
		sort : function (status, id){
	        var rInd = mygrid.getRowId((status=='up'?(mygrid.getRowIndex(id)-1):(mygrid.getRowIndex(id)+1)));
	        if(!rInd)	return ;
	        layoutProgressOn(dhxLayout);
	        roleBusinessService.selectTRoleBusinessSortNum(rInd, function(data){
				if(data != null){
					roleBusinessService.sortRoleBusiness(data, id,function(data){
						if(status == 'up'){
							mygrid.moveRowUp(id);
							mygrid.cellById(mygrid.getRowId(mygrid.getRowIndex(id)),0).setValue(mygrid.getRowIndex(id)+1);
							mygrid.cellById(mygrid.getRowId(mygrid.getRowIndex(id)+1),0).setValue(mygrid.getRowIndex(id)+2);
						} else {
							mygrid.moveRowDown(id);
							mygrid.cellById(mygrid.getRowId(mygrid.getRowIndex(id)-1),0).setValue(mygrid.getRowIndex(id));
							mygrid.cellById(mygrid.getRowId(mygrid.getRowIndex(id)),0).setValue(mygrid.getRowIndex(id)+1);
						}
						parent.notifyCreater('排序成功');
						layoutProgressOff(dhxLayout);
					});
				}else{
					parent.notifyWarn('排序发生错误');
					layoutProgressOff(dhxLayout);
					return;
				}
			});
		},
		addUsers : function (){
			if(mygrid.getSelectedId()){
				addUserWindow = openNewWindowURL('管理用户','<%=basePath%>page/system/roleBusiness/roleBusinessUserList.jsp?roleBusinessId='+mygrid.getSelectedId());
			}else parent.notifyWarn('请选择一个业务角色');
		}
	};
	
	function closeUserWindow(){
		if(addUserWindow){
			addUserWindow.close();
		}
	}
	
	function configLayout(){
		var a = dhxLayout.cells('a');
		a.hideHeader();
		a.setText('主页面');
		a.fixSize(true, true);
		mygrid = createGridFromLayoutCell(a);
		if(power.getPower() != '' && (power.getPowerInsert() || power.getPowerDelete()))
			createToolbarFromDHXLayoutCell(a,function(toolbar){
			    toolbar.addText("info", 1, "业务角色列表:");
			    toolbar.addSeparator("sep1", 2);
			    if(power.getPowerInsert())toolbar.addButton("add", 3, "新增业务角色", "new.gif", "new_dis.gif");
		    	if(power.getPowerDelete())toolbar.addButton("delete", 4, "删除业务角色", "cut.gif", "cut_dis.gif");
		    	if(power.getPowerUpdate()){
		    		toolbar.addButton('updateUp',5,'上移业务角色','arrow_up.png','arrow_up.png');
		    		toolbar.addButton('updateDown',6,'下移业务角色','arrow_down.png','arrow_down.png');
		    	}
		    	if(power.getPowerInsert())toolbar.addButton("addUsers", 7, "编辑角色用户", "group_link.png", "group_link.png");
		    	toolbar.attachEvent("onClick", function(id) {
			    	if(id == 'add'){
			    		dhxGrid.createInfo();
			    	}
			    	if(id == 'delete'){
			    		dhxGrid.deleteInfo();
			    	}
			    	if(id == 'updateUp'){
			    		dhxGrid.rowUp();
			    	}
			    	if(id == 'updateDown'){
			    		dhxGrid.rowDown();
			    	}
			    	if(id == 'addUsers'){
			    		dhxGrid.addUsers();
			    	}
			    });
			});
	}
	
	$(function (){
		configLayout();
	 	dhxGrid.initDataTable();
	});
	</script>
</head>

<body>
</body>
 
</html>

